package com.hy.solr.controller;

import com.hy.solr.entity.ModelEntity;
import com.hy.solr.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/model/")
@Api(value = "测试mode0l.API")
public class ModelController {
    @Autowired
    private ModelService modelService;

    /**
     * 保存
     */
    @ApiOperation(value = "保存接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity<?> save(@RequestBody ModelEntity modelEntity) {
        return ResponseEntity.ok(modelService.insert(modelEntity));
    }
    /**
     * 编辑
     */
    @ApiOperation(value = "编辑接口")
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public ResponseEntity<?> edit(@RequestBody ModelEntity modelEntity) {
        return ResponseEntity.ok(modelService.update(modelEntity));
    }
    /**
     * 删除
     */
    @ApiOperation(value = "删除接口")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<?> delete(@PathVariable String id) {
        return ResponseEntity.ok(modelService.delete(id));
    }
    /**
     * 查询列表
     */
    @ApiOperation(value = "查询接口")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResponseEntity<?> page() {
        return ResponseEntity.ok(modelService.select());
    }

    /**
     * 查询列表
     */
    @ApiOperation(value = "批量导出接口")
    @RequestMapping(value = "/poiout", method = RequestMethod.GET)
    public void poiout(Integer page,Integer pagesize,
                       HttpServletRequest servletRequest,
                       HttpServletResponse response) throws IOException, SolrServerException {
        String filedisplay = "门牌批量导出.xlsx";
        XSSFWorkbook xssfWorkbook = modelService.poiout(page,pagesize);
        servletRequest.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/x-download");
        filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
        response.addHeader("Content-disposition", "attachment;filename=" + filedisplay);//默认Excel名称
        xssfWorkbook.write(response.getOutputStream());
    }
}
